using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using System.Transactions;
using GC2.Entities.Admin;
using GC2.ExceptionHandler.Services;
using GC2.DataAccess.DAAdmin;

namespace GC2.Bussines.BMAdmin
{
    public class BMStock
    {
        public BMStock(string coneccion) 
        {
            this.Connection = coneccion;
        }

        private string connection = "";
        public string Connection
        {
            get { return connection; }
            set { connection = value; }
        }
                
        List<Stock> listaStock = new List<Stock>();
        Stock oStock = new Stock();
        DAStock daStock = new DAStock();

        #region BMStockSearch
        public List<Stock> BMStockSearch(Stock articulo)
        {
            try
            {
                listaStock = daStock.DAStockSearch(connection, articulo);
                return listaStock;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockSearchCodBarr
        public List<Stock> BMStockSearchCodBarr(Stock articulo)
        {
            try
            {
                listaStock = daStock.DAStockSearchCodBarr(connection, articulo);
                return listaStock;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockGetAll
        public List<Stock> BMStockGetAll(Stock articulo)
        { 
        try
            {
                listaStock = daStock.DAStockSearch(connection, articulo);
                return listaStock;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockGetAllLotes
        /// <summary>
        /// Genera un articulo por cada Lote
        /// </summary>
        /// <param name="articulo"></param>
        /// <returns>List<Stock></returns>
        public List<Stock> BMStockGetAllLotes(Stock articulo)
        {
            try
            {
                DALotes daLotes = new DALotes();
                listaStock = new List<Stock>();
                List<Stock> listaTemp = new List<Stock>();
                listaTemp = daStock.DAStockSearch(connection, articulo);
                foreach (Stock stock in listaTemp)
                {
                    if (stock.UsaLotes == 1)
                    {
                        List<Lotes> listaLotes = new List<Lotes>();
                        Lotes lote = new Lotes();
                        lote.Id_Rubro = stock.Id_Rubro;
                        lote.Id_SubRubro = stock.Id_SubRubro;
                        lote.Id_Articulo = stock.Id_Articulo;
                        lote.Estado = 1;
                        listaLotes = daLotes.DALotesSearch(Connection, lote);
                        if (listaLotes.Count > 0)
                        {
                            foreach (Lotes item in listaLotes)
                            {
                                Stock lStock = new Stock();
                                lStock.Articulo = stock.Articulo;
                                lStock.CodigoBarra = stock.CodigoBarra;
                                lStock.Estado = stock.Estado;
                                lStock.Id_Articulo = stock.Id_Articulo;
                                lStock.Id_Rubro = stock.Id_Rubro;
                                lStock.Id_SubRubro = stock.Id_SubRubro;
                                lStock.Lote = item.Lote; // Dato de Lote
                                lStock.FechaVto = item.FechaVto; // Dato de Lote
                                lStock.Precio = stock.Precio;
                                lStock.PuntoPedido = stock.PuntoPedido;
                                lStock.Rubro = stock.Rubro;
                                lStock.Saldo = item.Saldo; // Dato de Lote
                                lStock.SubRubro = stock.SubRubro;
                                lStock.UsaLotes = stock.UsaLotes;

                                listaStock.Add(lStock);
                            }
                        }
                        else
                        {
                            listaStock.Add(stock);
                        }
                    }
                    else
                    {
                        listaStock.Add(stock);
                    }
                }
                return listaStock;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockGetOne
        public Stock BMStockGetOne(Stock stock)
        {
            try
            {
                oStock = daStock.DAStockGetOne(connection, stock);
                return oStock;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockInsert
        public void BMStockInsert(Stock stock)
        {
            try
            {
                daStock.DAStockInsert(connection, stock);
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockInsertNuevo
        public void BMStockInsertNuevo(Stock stock)
        {
            try
            {
                if (stock.Id_Articulo == 0)
                {
                    stock.Id_Articulo = daStock.DAStockProximoId(connection, stock);
                }
                daStock.DAStockInsert(connection, stock);
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockUpdate
        public void BMStockUpdate(Stock stock)
        {
            try
            {
                daStock.DAStockUpdate(connection, stock);
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockUpdateSaldo
        public void BMStockUpdateSaldo(Stock stock)
        {
            try
            {
                oStock = new Stock();
                oStock.Id_Rubro = stock.Id_Rubro;
                oStock.Id_SubRubro = stock.Id_SubRubro;
                oStock.Id_Articulo = stock.Id_Articulo;
                oStock = BMStockGetOne(oStock);
                oStock.Saldo = oStock.Saldo - stock.Saldo;
                daStock.DAStockUpdateSaldo(connection, oStock);
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockSuma
        public void BMStockSuma(Stock stock)
        {
            try
            {
                Stock stockAnt = new Stock();

                stockAnt = daStock.DAStockGetOne(connection, stock);
                stockAnt.Saldo += stock.Saldo;
                daStock.DAStockUpdate(connection, stock);

                stockAnt = null;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

        #region BMStockResta
        public void BMStockResta(Stock stock)
        {
            try
            {
                Stock stockAnt = new Stock();

                stockAnt = daStock.DAStockGetOne(connection, stock);
                stockAnt.Saldo = stockAnt.Saldo - stock.Saldo;
                daStock.DAStockUpdate(connection, stock);

                stockAnt = null;
            }
            catch (Exception ex)
            {
                ExceptionBM exNew = new ExceptionBM();
                throw new System.Exception(exNew.exBM(ex.ToString()).ToString());
            }
        }
        #endregion

    }
}
